//
//  SetCityViewController.m
//  WeatherForecastDemo
//
//  Created by lizhiwei on 13-1-8.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "SetCity1ViewController.h"
#import "CityListViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"

@interface SetCity1ViewController ()

@end

@implementation SetCity1ViewController
@synthesize mytableView;
@synthesize list;
@synthesize cityButton;
@synthesize delegate;
@synthesize DoneButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSUserDefaults *weatherDefaults=[NSUserDefaults standardUserDefaults];
    NSMutableArray *arraryWeather=[weatherDefaults objectForKey:@"weatherArray"];
    if ([arraryWeather count]>0) {
        self.list=[[NSMutableArray alloc]initWithArray:arraryWeather];
    }
    else
    {
        NSMutableArray * array =[[NSMutableArray alloc] initWithObjects:@"深圳", @"北京", @"上海", nil];
        self.list = array;
    }
}

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了编辑");
    NSUInteger row = [indexPath row];
    UITableViewCell *cellCity=[mytableView cellForRowAtIndexPath:indexPath];
    if ([cellCity.textLabel.text isEqualToString:@"深圳"]) {
        [self justShowMessage:@"深圳的天气预报不能删除!" title:@"提示"];
        return;
    }
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.list removeObjectAtIndex:row];
        [mytableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                           withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        //我们实现的是在所选行的位置插入一行，因此直接使用了参数indexPath
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
        //同样，将数据加到list中，用的row
        [self.list insertObject:@"新添加的行" atIndex:row];
        [mytableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    }
    if ([self.list count]>0) {
        NSUserDefaults *weatherDefaults=[NSUserDefaults standardUserDefaults];
        [weatherDefaults setObject:self.list forKey:@"weatherArray"];
    }
}

/*-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSLog(@"手指搓动了");
 return UITableViewCellEditingStyleDelete;
 //return UITableViewCellEditingStyleNone;
 //return UITableViewCellEditingStyleInsert;
 }*/

-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/*-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 return YES;
 }*/

/*-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 {
 NSUInteger fromRow = [sourceIndexPath row];
 NSUInteger toRow = [destinationIndexPath row];
 
 id object = [list objectAtIndex:fromRow];
 [self.list removeObjectAtIndex:fromRow];
 [self.list insertObject:object atIndex:toRow];
 
 }*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [list count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tag"];
    if(cell == nil)
    {
        cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tag"] autorelease];
    }
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell.accessoryType = UITableViewCellAccessoryNone;
    //cell.showsReorderControl = YES;
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [list objectAtIndex:row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (IBAction)pressCityButton:(id)sender
{
    //launch city list view
    CityListViewController *detailViewController = [[CityListViewController alloc] initWithNibName:@"CityListViewController" bundle:nil];
    
    detailViewController.delegate = self;
    
    detailViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:detailViewController animated:YES];
    [detailViewController release];
    
}

//CityListViewController protocol
- (void) citySelectionUpdate:(NSString*)selectedCity
{
    //[cityButton setTitle:selectedCity forState:UIControlStateNormal];
    NSUserDefaults *weatherDefaults=[NSUserDefaults standardUserDefaults];
    NSMutableArray *arrayCity=[weatherDefaults objectForKey:@"weatherArray"];
    BOOL hasCity=NO;
    for (int i=0; i<[arrayCity count]; i++) {
        NSString *stringCity=[arrayCity objectAtIndex:i];
        if ([stringCity isEqualToString:selectedCity]) {
            hasCity=YES;
            [self justShowMessage:@"您已经添加该城市!" title:@"注意"];
            break;
        }
    }
    if (hasCity==YES) {
        return;
    }
    [self.list insertObject:selectedCity atIndex:0];
    if ([self.list count]>0) {
        [weatherDefaults setObject:self.list forKey:@"weatherArray"];
    }
    [mytableView reloadData];
}

- (NSString*) getDefaultCity
{
    return cityButton.titleLabel.text;
}

- (IBAction)DoneButton:(id)sender
{
     //[self dismissModalViewControllerAnimated:YES];
    WeatherForecastViewController *viewcontroller = [[WeatherForecastViewController alloc]init]; 
    //initWithNibName:@"WeatherForecastViewController" bundle:nil];
    
    viewcontroller.delegate = self;
    [viewcontroller initView];
    viewcontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}

//ViewController protocol
- (NSString*) getPageCount
{
    NSString * Pagecount = [NSString stringWithFormat:@"%d",[list count]];
    return Pagecount;
}

-(NSMutableArray*) citysSelection
{
    return self.list;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload
{
    [self setCityButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end
